from sklearn.model_selection import GridSearchCV
from sklearn.neighbors import KNeighborsClassifier
from sklearn import datasets

grid_param = [
    {
        'weights': ['uniform'],
        'n_neighbors': [i for i in range(1, 11)]
    },
    {
        'weights': ['distance'],
        'n_neighbors': [i for i in range(1, 11)],
        'p': [i for i in range(1, 6)]
    }
]

digits = datasets.load_digits()
x = digits.data
y = digits.target

knn = KNeighborsClassifier()
grid_search = GridSearchCV(knn, grid_param, n_jobs=-1, verbose=2)
grid_search.fit(x, y)
# 最佳算法对象
estimator_ = grid_search.best_estimator_
# 最佳分数
score_ = grid_search.best_score_
# 最佳参数
params_ = grid_search.best_params_
print(params_)
